<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="hAdd">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色">

          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" />
            <span v-else> {{ row.name }} </span>
          </template>

        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">

          <template slot-scope="{ row }">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else> {{ row.state === 1 ? '已启用' : '未启用' }} </span>
          </template>

        </el-table-column>
        <el-table-column align="center" label="描述">

          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else> {{ row.description }} </span>
          </template>

        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150"
        >
          <template slot-scope="{ row }">
            <template v-if="row.isEdit">
              <el-button type="text" size="small" @click="hOK(row)">确认</el-button>
              <el-button type="text" size="small" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="small" @click="hPermission(row.id)">分配权限</el-button>
              <el-button type="text" size="small" @click="row.isEdit=true">编辑</el-button>
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容确定删除吗？"
                @onConfirm="hDel(row.id)"
              >
                <el-button slot="reference" type="text" size="small">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :current-page="page"
          :page-sizes="[3,5,7]"
          :background="true"
          layout="prev, pager, next, sizes"
          :total="total"
          :page-size="pagesize"
          @current-change="hCurrentChange"
          @size-change="hSizeChange"
        />
      </el-row>
      <!-- 表单 -->
      <el-dialog width="500px" title="新增角色" :visible.sync="isShow">
        <!-- 表单内容 -->
        <el-form ref="form" label-width="120px" :model="form" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item label="启用">
            <el-switch
              v-model="form.state"
              size="mini"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" type="textarea" :rows="3" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item>
            <el-row type="flex" justify="center">
              <el-col :span="12">
                <el-button type="primary" size="mini" @click="addRole">确定</el-button>
                <el-button size="mini">取消</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <!-- 子组件 -->
    <assignPermission ref="refsRoles" :cur-id="curId" :show-dialog-assign.sync="showDialogAssign" />
  </div>
</template>

<script>
import { getRoleList, getRole, updatePut, updateDel } from '@/api/rols'
import assignPermission from './components/assignPermission.vue'
export default {
  name: 'AppContainer',
  components: { assignPermission },
  data() {
    return {
      curId: -1,
      showDialogAssign: false,
      isEdit: false,
      isShow: false,
      list: [],
      total: 0, // 总条数
      page: 1, // 页数
      pagesize: 5, // 每一页的条数
      form: {
        name: '测试',
        state: 1,
        description: '描述信息'
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色信息不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    //  封装分页
    this.getRole()
  },
  methods: {
    hPermission(id) {
      this.showDialogAssign = true

      this.curId = id

      this.$nextTick(() => {
        this.$refs.refsRoles.getgetRoleIdList()
      })
    },
    // 添加-角色
    async addRole() {
      // 发送 添加 请求
      const res = await getRole(this.form)
      console.log(res)
      // 提醒用户
      this.$message.success('添加成功')
      //  关闭弹框
      this.isShow = false
      //  清空表单
      this.$refs.form.resetFields()
      //  重新渲染  Math.ceil() 向上取整
      this.total = this.total + 1
      this.page = Math.ceil(this.total / this.pagesize)
      // 重新渲染
      this.getRole()
    },
    // 删除-角色
    async hDel(id) {
      // 发送删除角色请求
      const res = await updateDel(id)
      console.log(res)
      // 提醒用户删除陈宫
      this.$message.success('删除成功')
      if (this.list.length === 1) {
        if (this.page > 1) { this.page-- }
      }
      // 重新渲染
      this.getRole()
    },
    // 修改-角色
    async hOK(row) {
      if (!row.editRow.name || row.editRow.description) {
        return
      }
      const res = await updatePut(this.rows)
      console.log(res)
      // 提示用户
      this.$message.success('修改角色成功')
      // 退出编辑
      // eslint-disable-next-line require-atomic-updates
      row.isEdit = false
      // 重新渲染
      this.getRole()
    },
    hEdit(row) {
      console.log(row)
      row.isEdit = true
    },
    hAdd() {
      this.isShow = true
    },
    hSizeChange(pages) {
      // 用户点击的哪条 page 数
      console.log(pages)
      // 1. 更新每页显示的条数
      this.pagesize = pages

      // 2. 更新页码为1
      this.page = 1
      // 3. 重发请求
      this.getRole()
    },
    hCurrentChange(page) {
      console.log(page) // 每页的编码数
      // 更新页码
      this.page = page
      // 重新调用
      this.getRole()
    },
    async getRole() {
      //  获取角色列表   page是分页  pagesize是每天多少行
      const res = await getRoleList({ page: this.page, pagesize: this.pagesize })
      console.log(res.data.rows)
      this.list = res.data.rows
      // 每一个添加 forEach
      this.list.forEach(item => {
        console.log(item)
        //  $set
        // 参数1: 对象
        // 参数2: 要添加的属性名
        // 参数3: 要添加的属性值
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          id: item.id,
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
      this.total = res.data.total
    }
  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
